package handler

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"zg3/developer-branch/api/request"
	"zg3/developer-branch/consts"
	"zg3/developer-branch/internal/dao"
	"zg3/developer-branch/pkg"
)

func UserLogin(c *gin.Context) {
	var req request.UserLoginReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	user := dao.QueryUser(req.Account)
	if user.Id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "账号不存在",
			"data": false,
		})
		return
	}
	if user.Password != req.Password {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "密码输入错误",
			"data": false,
		})
		return
	}
	token, _ := pkg.NewJWT(consts.JWT_App_Key).CreateToken(pkg.CustomClaims{
		ID: user.Id,
	})

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "登录成功",
		"data": token,
	})
	return
}
